package com.eascs.admin.o2o.compensatory.controller;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.compensatory.entity.CompensatoryDataEnter;
import com.eascs.admin.o2o.compensatory.entity.O2oDCLoanVo;
import com.eascs.admin.o2o.compensatory.service.O2oDCLoanService;
import com.eascs.admin.o2o.compensatory.service.O2oDCStagesService;
import com.eascs.admin.o2o.detail.service.LoanDetailService;
import com.eascs.admin.o2o.loan.entity.O2oEaLoanDataEnity;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.loan.entity.O2oBankLoanData;
import com.eascs.loan.service.O2oLoanService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.Format;

@Controller
@RequestMapping("/dcloan")
public class O2oDCLoanController extends BaseMasterViewController {

	@Autowired
	O2oDCLoanService service;
	@Autowired
	O2oDCStagesService stagesService;
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Autowired
	private O2oLoanService o2oLoanService;
	@Autowired
	private LoanDetailService loanDetailService;

	@RequestMapping("/index")
	public String index(O2oDCLoanVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "compensatoryDataEntry");
		vo.setPageSize(10);
		String o2ourl = "/dcloan/index";
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();
		String loanerName = vo.getLoanerName() == null ? "" : vo.getLoanerName();
		String startdate = vo.getStartdate() == null ? "" : vo.getStartdate();
		String enddate = vo.getEnddate() == null ? "" : vo.getEnddate();
		String payType = vo.getPayType() == null ? "" : vo.getPayType();
		String province = vo.getProvince() == null ?  "" :vo.getProvince();
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanerName", loanerName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "startdate", startdate);
		o2ourl = UrlUtil.appendUrl(o2ourl, "enddate", enddate);
		o2ourl = UrlUtil.appendUrl(o2ourl, "payType", payType);
		o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
		o2ourl = o2ourl + "&pageIndex=";
		
		PageResult<Object[]> pageResult = service.getDCByPage(vo);
		String titles[] = { "贷款单号", "省份", "借款人姓名", "经办平台", "代偿类型","代偿时间","状态" };
		Format[] formats = new Format[] { null, null, null, null, null, null, null, null};
		SelectDataOption[] selectDataOptions = { null, null, null, null,new SelectDataOption("OTO_PAY_TYPE"), null,new SelectDataOption("PLACEONFILE")};
//		SelectDataOption[] selectDataOptions = { null, new SelectDataOption("O2OPROVINCE"), null, null,new SelectDataOption("OTO_PAY_TYPE"), null,new SelectDataOption("PLACEONFILE")};

		Button enter = ButtonHelper.createAjaxButton("录入", "/rdcloan/toEnterCabinetData?loanDataId=");
		Button update = ButtonHelper.createAjaxButton("修改", "/rdcloan/toUpdateCabinetData?loanDataId=");
		Button look = ButtonHelper.createOpenButton("查看", "/dcloan/baseInfo?billCode={1}");
		enter.setShowButtonFilter(showBottun1);
		enter.setPermissionCode("dczllrInputButton");

		update.setShowButtonFilter(showBottun2);
		update.setPermissionCode("dczllrUpdateButton");

		look.setShowButtonFilter(showBottun2);
		look.setPermissionCode("dczllrCheckButton");

		Button[] titleButton = new Button[] {ButtonHelper.createOpenButton(null, "/dcloan/baseInfo?billCode={1}")};
		Button[] dealButton = new Button[] { enter, update, look };

		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setTitleButtons(titleButton);
		o2oPagingInfo.setMoreButtons(dealButton);
		o2oPagingInfo.setFormats(formats);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "/dcloan/index";
	}
	
	/**
	 * 是否显示录入按钮过滤器
	 */
	public ShowButtonFilter showBottun1 = new ShowButtonFilter() {
		
		@Override
		public boolean filter(Object param) {
			Object[] params = (Object[]) param;
			CompensatoryDataEnter entity = service.findloanDataIdById(params[0].toString());
			if ( entity == null ) {
				return true;
			} else {
				return false;
			}
		}
	};
	
	/**
	 * 是否显示修改、查看按钮过滤器
	 */
	public ShowButtonFilter showBottun2 = new ShowButtonFilter() {
		
		@Override
		public boolean filter(Object param) {
			Object[] params = (Object[]) param;
			CompensatoryDataEnter entity = service.findloanDataIdById(params[0].toString());
			if ( entity == null ) {
				return false;
			} else {
				return true;
			}
		}
	};
	
	/**
	 * @Description:
	 *               <p>
	 *               代偿分期计划查询
	 *               </p>
	 * @author LMX
	 * @date 2017年8月29日 下午3:07:53
	 * @version V1.0
	 * @param vo
	 * @return
	 */
	@RequestMapping("/dcStagesFind")
	public String dcStagesFind(O2oDCLoanVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "DCStages");
		vo.setPageSize(10);
		String o2ourl = "/dcloan/dcStagesFind";
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();
		String loanerName = vo.getLoanerName() == null ? "" : vo.getLoanerName();
		String startdate = vo.getStartdate() == null ? "" : vo.getStartdate();
		String enddate = vo.getEnddate() == null ? "" : vo.getEnddate();
		o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanerName", loanerName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "startdate", startdate);
		o2ourl = UrlUtil.appendUrl(o2ourl, "enddate", enddate);
		o2ourl = o2ourl + "&pageIndex=";

//		PageResult<Object[]> pageResult = stagesService.getDCStagesByPage();
		PageResult<Object[]> pageResult = stagesService.getDCStagesByPage(vo);
		String titles[] = { "贷款单号", "借款人姓名", "经办平台", "信贷经理", "分期本金金额", "分期期数", "第一次计划还款时间", "创建时间" };
		Format[] formats = new Format[] { null, null, null, null, null, null, null, null };
		SelectDataOption[] selectDataOptions = { null, null, null, null, null, null, null, };
		Button[] titleButton = new Button[] { ButtonHelper.createOpenButton(null, "/back/detail?backId=") };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setTitleButtons(titleButton);
		o2oPagingInfo.setBuildPagingNavigation(true);// 底部显示分页页码
		o2oPagingInfo.setFormats(formats);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "/dcloan/dc_stages";
	}
	
	@RequestMapping("/baseInfo")
	public String getBaseInfo(String billCode){
		O2oLoanapplyEntity loanapply = o2oloanapplyService.fingByBillCodeDetail(billCode);
		String loanapplyNo = loanapply.getLoanapplyNo();
		loanapply = o2oloanapplyService.findByIdDetail(loanapplyNo);
		O2oBankLoanData bankData = o2oLoanService.queryBillCode(loanapply.getBillCode());
		O2oLoanUserEntity loanUser = o2oloanapplyService.findLoanUserEntity(loanapply.getLoanapplyNo(), loanapply.getLoanId());
		String isNotContinue = null;
		O2oLoanapplyEntity loanapplyEntity = loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(), loanapply.getLoanId(), loanapply.getBillCode(), loanapply.getCreateDate());
		if (loanapplyEntity != null) {
			loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
			isNotContinue = "Y";
		}
		O2oEaLoanDataEnity loanData = service.findEaLoanData(billCode);
		CompensatoryDataEnter entity = service.findloanDataIdById(loanData.getId());
		String flag = "false";
		if(entity != null){
			flag = "true";
		}
		
		ViewContext.put("flag", flag);
		ViewContext.put("vo", entity);
		ViewContext.put("isNotContinue", isNotContinue);
		ViewContext.put("loanapply", loanapply);
		ViewContext.put("bankData", bankData);
		ViewContext.put("loanUser", loanUser);
		return "/dcloan/dcloan_index";
	}
}
